<?php

namespace app\api\logic;

use think\facade\Db;
use app\data\util\HttpUtil;
use think\exception\ValidateException;
use think\facade\Cache;
use app\api\logic\ApiOrderLogic;

use app\systems\logic\MqTaskPushLogic;
use app\data\util\MqQueueName;

class WebhookLogic
{
    public function __construct()
    {

    }

    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 创建订单
     * @author zhangheng
     */
    public function orders_create($params = [])
    {
        //TODO目前只有Shecurve站用到webhooks
        // $params['shop_id'] = 17;

        MqTaskPushLogic::mq_task_push(
            '',
            MqQueueName::QUEUE_ORDER_WEBHOOK_QUEUE,
            $params,
            false
        );

        return json(DefaultResponse(200, true, '操作成功', ''));
    }
    /**
    * @desc    webhooks订单新增消费方法
    * @date    2024/7/16
    * @author  zhangheng
    */
    public function orders_creates_consumer($params = [])
    {

        $ApiOrderLogic    = new ApiOrderLogic();

        $arr['shop_id']   = $params['shop_id'] ?? 0;
        $arr['order_id']  = $params['id'] ?? 0;
        $arr['json']      = json_encode($params,true);
        $arr['timestamp'] = time();
        $arr['type']      = 1;

        DB::name('order_webhooks')->insert($arr);
        Db::startTrans();
        try {
            //新增订单
            if(!empty($arr['order_id'])){
                $row = Db::name('order')->where('order_id',$arr['order_id'])->find();
                if(empty($row)){
                    $ApiOrderLogic->SynchronizeModifications($params,$params['shop_id']);
                }
            }
            Db::commit();
        } catch (ValidateException $e) {
        } catch (\Exception $e) {
            Db::rollback();
        }
        return true;
    }
    
    /**
     * @param $params
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @desc 修改订单
     * @author zhangheng
     */
    public function orders_updated($params = [])
    {
        //TODO目前只有Shecurve站用到webhooks
        // $params['shop_id'] = 17;

        MqTaskPushLogic::mq_task_push(
            '',
            MqQueueName::QUEUE_ORDER_WEBHOOK_EDIT_QUEUE,
            $params,
            false
        );
        return json(DefaultResponse(200, true, '操作成功', ''));
    
    }
    /**
    * @desc    实时记录今天业绩
    * @date    2024/7/16
    * @author  zhangheng
    */
    public function orders_updateds($params = [])
    {
        $ApiOrderLogic = new ApiOrderLogic();

        $arr['shop_id'] = $params['shop_id'] ?? 0;
        $arr['order_id'] = $params['id'] ?? 0;
        $arr['json'] = json_encode($params,true);
        $arr['timestamp'] = time();
        $arr['type'] = 2;
        table('order_webhooks')->insert($arr);

        Db::startTrans();
        try {
            //新增修改订单
            if(!empty($arr['order_id'])){
                $row = Db::name('order')->where('order_id',$arr['order_id'])->find();
                if(empty($row)){
                    $ApiOrderLogic->SynchronizeModifications($params,$params['shop_id']);
                }else{
                    //更新时间更大就更新
                    $updated_at = $ApiOrderLogic->dateTimeStr($params['updated_at']);
                    $old_updated_at = $ApiOrderLogic->dateTimeStr($row['updated_at']);
                    if($updated_at >= $old_updated_at){//更新交易不会改变更新时间
                        $ApiOrderLogic->SynchronizzeModifications($params,$params['shop_id']);
                    }
                }
            }
            Db::commit();
        } catch (ValidateException $e) {
        } catch (\Exception $e) {
            Db::rollback();
        }

        return  true;
        
    }
}
